"use strict";

var uk = require('./def.js');

uk.monsterai = {};

/** 普通智能，有啥出啥了 */
uk.monsterai.normal = {
    /** 我的回合到了
     * src 回合到了
     */
    on_turn:function(src){
        src.aitimer = uk.start_timer(uk.rand(500,2000),this.proc_outcard,this,src);
    },
    stop:function(src){
        if(src.aitimer){
            uk.stop_timer(src.aitimer);
            src.aitimer = null;
        }
    },
    proc_outcard:function(src){
        var hc = src.handcards;
        if(hc.length > 0){
            var target=null;
            for(var i in src.room.players){
                var p = src.room.players[i];
                if(p && p.team != src.team){
                    target = p;
                    break;
                }
            }
            if(target == null){
                src.room.finish_round();
                return;
            }
            var anyout = false;
            for(var i in src.handcards){
                var c = uk.t_cards[src.handcards[i]];
                var can = true;
                for(var ii in c.cost){
                    if(src.props[ii] < c.cost[ii]){
                        can = false;
                        break;
                    }
                }
                if(can){/** 下一次随机 */
                    src.aitimer = uk.start_timer(uk.rand(300,600),this.proc_outcard,this,src);
                    src.room.player_outcard(src,src.handcards[i],target.id);
                    anyout = true;
                    break;
                }
            }
            if(!anyout)
                src.room.finish_round();
        }else{
            src.room.finish_round();
        }
    }
}